{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "hello_mx_gpu_true.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python2",
      "display_name": "Python 2"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "[View in Colaboratory](https://colab.research.google.com/github/utensil/julia-playground/blob/master/dl/hello_mx_gpu_true.ipynb)"
      ]
    },
    {
      "metadata": {
        "id": "XgovBHwwtxyV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "9a0e4af3-01b1-4e96-9fdf-b6601cbc3207"
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "device_name = tf.test.gpu_device_name()\n",
        "if device_name != '/device:GPU:0':\n",
        "  raise SystemError('GPU device not found')\n",
        "print('Found GPU at: {}'.format(device_name))"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Found GPU at: /device:GPU:0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "O-peEN9Mt19e",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 541
        },
        "outputId": "b0c22b57-96cc-452b-867b-78ad4a062f4f"
      },
      "cell_type": "code",
      "source": [
        "!apt install libnvrtc8.0\n",
        "!pip install mxnet-cu80\n",
        "import mxnet as mx"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Reading package lists... Done\n",
            "Building dependency tree       \n",
            "Reading state information... Done\n",
            "The following NEW packages will be installed:\n",
            "  libnvrtc8.0\n",
            "0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\n",
            "Need to get 6,225 kB of archives.\n",
            "After this operation, 28.3 MB of additional disk space will be used.\n",
            "Get:1 http://archive.ubuntu.com/ubuntu artful/multiverse amd64 libnvrtc8.0 amd64 8.0.61-1 [6,225 kB]\n",
            "Fetched 6,225 kB in 1s (5,420 kB/s)\n",
            "\n",
            "\u001b7\u001b[0;23r\u001b8\u001b[1ASelecting previously unselected package libnvrtc8.0:amd64.\n",
            "(Reading database ... 86790 files and directories currently installed.)\n",
            "Preparing to unpack .../libnvrtc8.0_8.0.61-1_amd64.deb ...\n",
            "\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [  0%]\u001b[49m\u001b[39m [..........................................................] \u001b8\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [ 16%]\u001b[49m\u001b[39m [#########.................................................] \u001b8Unpacking libnvrtc8.0:amd64 (8.0.61-1) ...\n",
            "\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [ 33%]\u001b[49m\u001b[39m [###################.......................................] \u001b8\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [ 50%]\u001b[49m\u001b[39m [#############################.............................] \u001b8Setting up libnvrtc8.0:amd64 (8.0.61-1) ...\n",
            "\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [ 66%]\u001b[49m\u001b[39m [######################################....................] \u001b8\u001b7\u001b[24;0f\u001b[42m\u001b[30mProgress: [ 83%]\u001b[49m\u001b[39m [################################################..........] \u001b8Processing triggers for libc-bin (2.26-0ubuntu2.1) ...\n",
            "\n",
            "\u001b7\u001b[0;24r\u001b8\u001b[1A\u001b[JCollecting mxnet-cu80\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/f6/6c/566a1d4b8b1005b7d9ccfaecd7632f6dca596246f6657827b2d4e97c72c7/mxnet_cu80-1.2.1-py2.py3-none-manylinux1_x86_64.whl (299.1MB)\n",
            "\u001b[K    22% |███████▏                        | 67.1MB 29.7MB/s eta 0:00:08"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "\u001b[K    100% |████████████████████████████████| 299.1MB 47kB/s \n",
            "\u001b[?25hRequirement already satisfied: graphviz<0.9.0,>=0.8.1 in /usr/local/lib/python2.7/dist-packages (from mxnet-cu80) (0.8.4)\n",
            "Requirement already satisfied: numpy<1.15.0,>=1.8.2 in /usr/local/lib/python2.7/dist-packages (from mxnet-cu80) (1.14.5)\n",
            "Requirement already satisfied: requests<2.19.0,>=2.18.4 in /usr/local/lib/python2.7/dist-packages (from mxnet-cu80) (2.18.4)\n",
            "Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests<2.19.0,>=2.18.4->mxnet-cu80) (2.6)\n",
            "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests<2.19.0,>=2.18.4->mxnet-cu80) (1.22)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests<2.19.0,>=2.18.4->mxnet-cu80) (2018.4.16)\n",
            "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests<2.19.0,>=2.18.4->mxnet-cu80) (3.0.4)\n",
            "Installing collected packages: mxnet-cu80\n",
            "Successfully installed mxnet-cu80-1.2.1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "sFQW9UHCuALn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "From https://gluon.mxnet.io/chapter03_deep-neural-networks/mlp-gluon.html :"
      ]
    },
    {
      "metadata": {
        "id": "GoqGijnKuPV6",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from __future__ import print_function\n",
        "import numpy as np\n",
        "import mxnet as mx\n",
        "from mxnet import nd, autograd, gluon\n",
        "\n",
        "data_ctx = mx.cpu()\n",
        "model_ctx = mx.gpu(0)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "3Ov4747xuTwj",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "batch_size = 64\n",
        "num_inputs = 784\n",
        "num_outputs = 10\n",
        "num_examples = 60000\n",
        "def transform(data, label):\n",
        "    return data.astype(np.float32)/255, label.astype(np.float32)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "uvATYmJbuWHP",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True, transform=transform),\n",
        "                                      batch_size, shuffle=True)\n",
        "test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),\n",
        "                                     batch_size, shuffle=False)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Lfa0UlDyuXbm",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "class MLP(gluon.Block):\n",
        "    def __init__(self, **kwargs):\n",
        "        super(MLP, self).__init__(**kwargs)\n",
        "        with self.name_scope():\n",
        "            self.dense0 = gluon.nn.Dense(64)\n",
        "            self.dense1 = gluon.nn.Dense(64)\n",
        "            self.dense2 = gluon.nn.Dense(10)\n",
        "\n",
        "    def forward(self, x):\n",
        "        x = nd.relu(self.dense0(x))\n",
        "        x = nd.relu(self.dense1(x))\n",
        "        x = self.dense2(x)\n",
        "        return x"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "OR1haNKquZPE",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "net = MLP()\n",
        "net.collect_params().initialize(mx.init.Normal(sigma=.01), ctx=model_ctx)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "yEiNxZsuueIU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "52a1f1c5-1ca4-458f-9796-c9e9ee5e0248"
      },
      "cell_type": "code",
      "source": [
        "data = nd.ones((1,784))\n",
        "net(data.as_in_context(model_ctx))"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.0010479  -0.00023263  0.00024665 -0.00137001 -0.00089217 -0.00043491\n",
              "   0.00174529 -0.00114445  0.00024293 -0.0004818 ]]\n",
              "<NDArray 1x10 @gpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "metadata": {
        "id": "lpxYToNhuhc8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 588
        },
        "outputId": "362c8654-b5ff-4732-bee9-e9ad58e27a8d"
      },
      "cell_type": "code",
      "source": [
        "class MLP(gluon.Block):\n",
        "    def __init__(self, **kwargs):\n",
        "        super(MLP, self).__init__(**kwargs)\n",
        "        with self.name_scope():\n",
        "            self.dense0 = gluon.nn.Dense(64, activation=\"relu\")\n",
        "            self.dense1 = gluon.nn.Dense(64, activation=\"relu\")\n",
        "            self.dense2 = gluon.nn.Dense(10)\n",
        "\n",
        "    def forward(self, x):\n",
        "        x = self.dense0(x)\n",
        "        print(\"Hidden Representation 1: %s\" % x)\n",
        "        x = self.dense1(x)\n",
        "        print(\"Hidden Representation 2: %s\" % x)\n",
        "        x = self.dense2(x)\n",
        "        print(\"Network output: %s\" % x)\n",
        "        return x\n",
        "\n",
        "net = MLP()\n",
        "net.collect_params().initialize(mx.init.Normal(sigma=.01), ctx=model_ctx)\n",
        "net(data.as_in_context(model_ctx))"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hidden Representation 1: \n",
            "[[0.         0.         0.07143721 0.         0.         0.02748545\n",
            "  0.33361885 0.2867228  0.         0.         0.5456277  0.21671814\n",
            "  0.20799856 0.2859339  0.         0.34327483 0.         0.4614671\n",
            "  0.02448964 0.         0.01388636 0.         0.25961956 0.\n",
            "  0.15838279 0.         0.10904857 0.         0.         0.02812487\n",
            "  0.         0.         0.3602645  0.         0.3662219  0.\n",
            "  0.36324638 0.19642079 0.35552478 0.01087478 0.6817804  0.\n",
            "  0.         0.02232164 0.02738917 0.         0.         0.\n",
            "  0.05049316 0.37144935 0.16642064 0.         0.07810777 0.01600346\n",
            "  0.         0.         0.         0.         0.         0.\n",
            "  0.         0.         0.07678016 0.6952986 ]]\n",
            "<NDArray 1x64 @gpu(0)>\n",
            "Hidden Representation 2: \n",
            "[[0.00646666 0.         0.01819706 0.         0.         0.00655819\n",
            "  0.         0.         0.         0.         0.         0.02194543\n",
            "  0.         0.         0.00290144 0.         0.         0.00796402\n",
            "  0.         0.         0.00674235 0.00493965 0.0087099  0.01728906\n",
            "  0.         0.00606559 0.         0.00021488 0.02582733 0.02659095\n",
            "  0.         0.         0.         0.01349063 0.00774558 0.\n",
            "  0.00166781 0.01252956 0.         0.         0.         0.02674095\n",
            "  0.         0.         0.00862347 0.0197845  0.         0.\n",
            "  0.02958972 0.         0.         0.         0.         0.\n",
            "  0.00199858 0.         0.         0.00343865 0.         0.03784892\n",
            "  0.01264702 0.         0.         0.        ]]\n",
            "<NDArray 1x64 @gpu(0)>\n",
            "Network output: \n",
            "[[ 0.00144656  0.00042265 -0.00119047  0.00066365  0.00035064  0.00032936\n",
            "  -0.00058919 -0.00066042 -0.00112773 -0.00013794]]\n",
            "<NDArray 1x10 @gpu(0)>\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.00144656  0.00042265 -0.00119047  0.00066365  0.00035064  0.00032936\n",
              "  -0.00058919 -0.00066042 -0.00112773 -0.00013794]]\n",
              "<NDArray 1x10 @gpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "metadata": {
        "id": "u3YolrZ1ukyD",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "num_hidden = 64\n",
        "net = gluon.nn.HybridSequential()\n",
        "with net.name_scope():\n",
        "    net.add(gluon.nn.Dense(num_hidden, activation=\"relu\"))\n",
        "    net.add(gluon.nn.Dense(num_hidden, activation=\"relu\"))\n",
        "    net.add(gluon.nn.Dense(num_outputs))\n",
        "net.hybridize()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "rEOK-rZ0unGo",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "net.collect_params().initialize(mx.init.Normal(sigma=.1), ctx=model_ctx)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ZAJBaTIfuobK",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "64HX9ph6uqBw",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .01})"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Mlm5Q7gLuriA",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def evaluate_accuracy(data_iterator, net):\n",
        "    acc = mx.metric.Accuracy()\n",
        "    for i, (data, label) in enumerate(data_iterator):\n",
        "        data = data.as_in_context(model_ctx).reshape((-1, 784))\n",
        "        label = label.as_in_context(model_ctx)\n",
        "        output = net(data)\n",
        "        predictions = nd.argmax(output, axis=1)\n",
        "        acc.update(preds=predictions, labels=label)\n",
        "    return acc.get()[1]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "6rCX8wWZutHn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 202
        },
        "outputId": "5cfc4bf5-b1bf-44a6-9ad1-3f4281388a51"
      },
      "cell_type": "code",
      "source": [
        "!pip install mxboard"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting mxboard\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/65/f5/a539f60776144d638bb2cb5b56c767da4359e8236652d29c586476f10a83/mxboard-0.1.0-py2-none-any.whl (47kB)\n",
            "\u001b[K    100% |████████████████████████████████| 51kB 2.3MB/s \n",
            "\u001b[?25hRequirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from mxboard) (1.11.0)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python2.7/dist-packages (from mxboard) (1.14.5)\n",
            "Requirement already satisfied: protobuf>=3.0.0 in /usr/local/lib/python2.7/dist-packages (from mxboard) (3.6.0)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python2.7/dist-packages (from mxboard) (4.0.0)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python2.7/dist-packages (from protobuf>=3.0.0->mxboard) (39.1.0)\n",
            "Requirement already satisfied: olefile in /usr/local/lib/python2.7/dist-packages (from Pillow->mxboard) (0.45.1)\n",
            "Installing collected packages: mxboard\n",
            "Successfully installed mxboard-0.1.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "4I52r69-uzPG",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from mxboard import SummaryWriter\n",
        "sw = SummaryWriter(logdir='logs', flush_secs=5)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "1Rk4HQIdu2di",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 185
        },
        "outputId": "fc61bb68-08c3-4812-db1c-0e0d43a2077e"
      },
      "cell_type": "code",
      "source": [
        "epochs = 10\n",
        "smoothing_constant = .01\n",
        "\n",
        "# collect parameter names for logging the gradients of parameters in each epoch\n",
        "params = net.collect_params()\n",
        "param_names = params.keys()\n",
        "global_step = 0\n",
        "\n",
        "for e in range(epochs):\n",
        "    cumulative_loss = 0\n",
        "    for i, (data, label) in enumerate(train_data):\n",
        "        data = data.as_in_context(model_ctx).reshape((-1, 784))\n",
        "        label = label.as_in_context(model_ctx)\n",
        "        with autograd.record():\n",
        "            output = net(data)\n",
        "            loss = softmax_cross_entropy(output, label)\n",
        " \n",
        "        sw.add_scalar(tag='cross_entropy', value=loss.mean().asscalar(), global_step=global_step)\n",
        "        if i == 0:\n",
        "            sw.add_image('minist_first_minibatch', data.reshape((batch_size, 1, 28, 28)), e)\n",
        "        if e == 0:\n",
        "            sw.add_graph(net)\n",
        "        grads = [i.grad() for i in net.collect_params().values()]\n",
        "        for i, name in enumerate(param_names):\n",
        "            sw.add_histogram(tag=name, values=grads[i], global_step=e, bins=1000)\n",
        "\n",
        "        global_step += 1\n",
        "        loss.backward()\n",
        "        trainer.step(data.shape[0])\n",
        "        cumulative_loss += nd.sum(loss).asscalar()\n",
        "\n",
        "        \n",
        "    \n",
        "    test_accuracy = evaluate_accuracy(test_data, net)\n",
        "    train_accuracy = evaluate_accuracy(train_data, net)\n",
        "    sw.add_scalar(tag='accuracy_curves', value=('train_acc', train_accuracy), global_step=e)\n",
        "    sw.add_scalar(tag='accuracy_curves', value=('valid_acc', test_accuracy), global_step=e);\n",
        "    print (\"Epoch %s. Loss: %s, Train_acc %s, Test_acc %s\" % (e, cumulative_loss/num_examples, train_accuracy, test_accuracy)) "
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 0. Loss: 0.45416317160924274, Train_acc 0.886166666667, Test_acc 0.8904\n",
            "Epoch 1. Loss: 0.367962058989207, Train_acc 0.90005, Test_acc 0.9012\n",
            "Epoch 2. Loss: 0.3281178052981695, Train_acc 0.909833333333, Test_acc 0.9114\n",
            "Epoch 3. Loss: 0.30091046256224313, Train_acc 0.916866666667, Test_acc 0.9165\n",
            "Epoch 4. Loss: 0.27957490228017173, Train_acc 0.92285, Test_acc 0.922\n",
            "Epoch 5. Loss: 0.2620397605220477, Train_acc 0.92685, Test_acc 0.925\n",
            "Epoch 6. Loss: 0.24704794867038726, Train_acc 0.9311, Test_acc 0.9298\n",
            "Epoch 7. Loss: 0.23364521889686585, Train_acc 0.934766666667, Test_acc 0.9327\n",
            "Epoch 8. Loss: 0.22148989040851594, Train_acc 0.938133333333, Test_acc 0.9346\n",
            "Epoch 9. Loss: 0.21068138181765875, Train_acc 0.940883333333, Test_acc 0.9377\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "cWwrFyWi7Y7E",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "eb3002a2-0753-4894-fe56-e88af64cad38"
      },
      "cell_type": "code",
      "source": [
        "!ls logs"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "accuracy_curves  events.out.tfevents.1532778751.2f6afaea2f18\r\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "sV12uPHoGJCx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 134
        },
        "outputId": "b8262b5b-3b0b-4a18-923a-63cbce695dca"
      },
      "cell_type": "code",
      "source": [
        "!zip -r logs.zip logs/"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "  adding: logs/ (stored 0%)\n",
            "  adding: logs/events.out.tfevents.1532778751.2f6afaea2f18 (deflated 58%)\n",
            "  adding: logs/accuracy_curves/ (stored 0%)\n",
            "  adding: logs/accuracy_curves/valid_acc/ (stored 0%)\n",
            "  adding: logs/accuracy_curves/valid_acc/events.out.tfevents.1532778960.2f6afaea2f18 (deflated 54%)\n",
            "  adding: logs/accuracy_curves/train_acc/ (stored 0%)\n",
            "  adding: logs/accuracy_curves/train_acc/events.out.tfevents.1532778960.2f6afaea2f18 (deflated 55%)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Huc3S0bs8hZp",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from google.colab import files\n",
        "files.download('logs.zip')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "i0qPQAjq8sdD",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c83a428b-f235-40df-8864-c4d8a4465639"
      },
      "cell_type": "code",
      "source": [
        "!ls -lhta logs.zip"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "-rw-r--r-- 1 root root 407M Jul 28 12:52 logs.zip\r\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ag-8_1L3-rbC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4a2ca354-51e3-4385-84fa-e2f52061b728"
      },
      "cell_type": "code",
      "source": [
        "!pwd"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content\r\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "7xr9EKmK8wAb",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "LOG_DIR = '/content/logs'\n",
        "get_ipython().system_raw(\n",
        "    'tensorboard --logdir {} --host 127.0.0.1 --port 6006 & || echo failed'\n",
        "    .format(LOG_DIR)\n",
        ")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "9Rhyh0j196Vg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "f8b160f8-5347-440c-87b6-8b53d7637b60"
      },
      "cell_type": "code",
      "source": [
        "! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n",
        "! unzip ngrok-stable-linux-amd64.zip"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "--2018-07-28 12:59:49--  https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n",
            "Resolving bin.equinox.io (bin.equinox.io)... 52.207.39.76, 52.22.213.157, 52.23.126.223, ...\n",
            "Connecting to bin.equinox.io (bin.equinox.io)|52.207.39.76|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 5363700 (5.1M) [application/octet-stream]\n",
            "Saving to: ‘ngrok-stable-linux-amd64.zip’\n",
            "\n",
            "ngrok-stable-linux- 100%[===================>]   5.11M  19.9MB/s    in 0.3s    \n",
            "\n",
            "2018-07-28 12:59:50 (19.9 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [5363700/5363700]\n",
            "\n",
            "Archive:  ngrok-stable-linux-amd64.zip\n",
            "  inflating: ngrok                   \n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "TIGoO2Ey-T6E",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "get_ipython().system_raw('./ngrok http 6006 &')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "2pjukdgA-fGY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "53c11d92-4921-41bc-9e59-616ab3cbaf3d"
      },
      "cell_type": "code",
      "source": [
        "! curl -s http://localhost:4040/api/tunnels | python3 -c \\\n",
        "    \"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\""
      ],
      "execution_count": 97,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "http://31c5710b.ngrok.io\r\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "jiTM7IzKDZRB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "f27164ee-fb1f-4b1f-c1fe-7e71e6497650"
      },
      "cell_type": "code",
      "source": [
        "!python /usr/local/lib/python2.7/dist-packages/tensorboard/main.py  --logdir /content/logs --host 0.0.0.0 --port 6006 2>&1|grep -v Reloader"
      ],
      "execution_count": 98,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "TensorBoard 1.9.0 at http://0.0.0.0:6006 (Press CTRL+C to quit)\n",
            "^C\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}